/***********************************************
 This file is part of redia-bapps-main-ios.
 
 Copyright (c) 2015 Redia A/S
 
 Redia-bapps-main-ios is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
 
 Redia-bapps-main-ios is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with redia-bapps-main-ios.  If not, see <http://www.gnu.org/licenses/>.
 
 *********************************************** */

//
//  BATabSwitchViewController.m
//  redia-bapps-ios
//
//  Created by Henrik Lynbech on 26/09/13.
//  Copyright (c) 2013 redia. All rights reserved.
//

#import "BATabSwitchViewController.h"
#import "BATabSwitchSegmentViewController.h"
#import "BAUtils.h"


@interface BATabSwitchViewController ()

@end

@implementation BATabSwitchViewController

-(id)initWithSelectionColor:(UIColor*)selectedColor unselectionColor:(UIColor*)unselectedColor{
    self = [super init];
    if (self) {
        _selectedColor = selectedColor;
        _unselectedColor = unselectedColor;
    }
    return self;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)setTitleStrings:(NSArray *)titleStrings
{
    for (BATabSwitchSegmentViewController* old_segment in segments) {
        [old_segment.view removeFromSuperview];
    }
    
    segments = [NSMutableArray new]; //discarding all old segments
    
    int index = 0;
    for (NSString* title in titleStrings) {
        BATabSwitchSegmentViewController* segment = [BATabSwitchSegmentViewController new];
        [self.view addSubview:segment.view];
        [segment setIndex:index];
        [segment setCount:titleStrings.count];
        [segment setSelectedColor:self.selectedColor];
        [segment setUnselectedColor:self.unselectedColor];
        /*
        segment.hideStartImage=TRUE;
        if (index == [titleStrings count]-1) {
            segment.hideEndImage=TRUE;
        }
        if (index==0) {
            segment.isSelected=TRUE;
        } else {
            segment.isSelected=FALSE;
        }
         */
        [segment.switchButton setTitle:title forState:UIControlStateNormal];
        [segment.switchButton addTarget:self action:@selector(segmentClicked:) forControlEvents:UIControlEventTouchUpInside];
        segment.switchButton.tag = index;
        
        [segments addObject:segment];
        
        index++;
        
    }
    selectedIndex = 0;
    
    [self relayoutSegments];
}

-(void)relayoutSegments
{
//    double segmentWidth = self.view.bounds.size.width / ((double)[segments count]);
//    
//    int index=0;
    UIView* previousView = nil;
    for (BATabSwitchSegmentViewController* segment in segments) {
        UIView* view = segment.view;
        view.translatesAutoresizingMaskIntoConstraints = NO;
        [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[view]|"
                                                                          options:0 metrics:nil
                                                                            views:NSDictionaryOfVariableBindings(view)]];
        if (segment == segments.firstObject) {
            [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[view]"
                                                                              options:0 metrics:nil
                                                                                views:NSDictionaryOfVariableBindings(view)]];
        } else{
            [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[previousView][view(==previousView)]"
                                                                              options:0 metrics:nil
                                                                                views:NSDictionaryOfVariableBindings(view, previousView)]];
        }
        previousView = view;
        /*
        segment.view.frame = CGRectMake(segmentWidth*index, 0, segmentWidth, 44);
        index++;
         */
    }
    if (previousView != nil) {
        [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[previousView]|"
                                                                          options:0 metrics:nil
                                                                            views:NSDictionaryOfVariableBindings(previousView)]];
    }
    
}

-(void)selectTabIndex:(int)newlySelectedIndex
{
    if (newlySelectedIndex>=0 && newlySelectedIndex<[segments count]) {
        selectedIndex = newlySelectedIndex;
        int index = 0;
        for (BATabSwitchSegmentViewController* segment in segments) {
            [segment setSelectedIndex:selectedIndex];
            index++;
        }
        
        BATabSwitchSegmentViewController* segmentcontroller = [segments objectAtIndex:selectedIndex];
        [self.delegate tabSwitchSelected:selectedIndex withTitle:segmentcontroller.switchButton.titleLabel.text];
    }

}

-(void)segmentClicked:(id)sender
{
    UIButton* segmentButton = sender;
    int index_from_tag = segmentButton.tag;
    [self selectTabIndex:index_from_tag];
}

-(void)setSelectedColor:(UIColor *)selectedColor{
    if (selectedColor != _selectedColor) {
        _selectedColor = selectedColor;
        for (BATabSwitchSegmentViewController* segment in segments) {
            [segment setSelectedColor:selectedColor];
        }
    }
}

-(void)setUnselectedColor:(UIColor *)unselectedColor{
    if (unselectedColor != _unselectedColor) {
        _unselectedColor = unselectedColor;
        for (BATabSwitchSegmentViewController* segment in segments) {
            [segment setUnselectedColor:unselectedColor];
        }
    }
}

-(void)setTitleString:(NSString *)string forIndex:(int)index
{
    if (index>=0 && index<segments.count) {
        BATabSwitchSegmentViewController* segment = [segments objectAtIndex:index];
        [segment.switchButton setTitle:string forState:UIControlStateNormal];
    }
}

@end
